package com.wzy.service.impl;

import com.wzy.entity.Blog;
import com.wzy.entity.Violence;
import com.wzy.mapper.ViolenceMapper;
import com.wzy.service.ViolenceService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wzy.util.Const;
import com.wzy.util.Pager;
import com.wzy.util.textTranfromHtmlUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author wzy
 * @since 2021-02-09
 */
@Service
public class ViolenceServiceImpl extends ServiceImpl<ViolenceMapper, Violence> implements ViolenceService {

    @Resource
    private ViolenceMapper violenceMapper;

    @Autowired
    RedisTemplate redisTemplate;

    @Override
    public List<Violence> listNotAppend() {
        return this.violenceMapper.selectNotAppend();
    }

    @Cacheable(key = "'violences'", value = "violence")
    @Override
    public List<Violence> getViolenceList() {
        return baseMapper.selectList(null).stream().filter(item->item.getViolenceId()!=0).collect(Collectors.toList());
    }

    @Override
    public boolean append(Violence violence) {
        return this.violenceMapper.inserts(violence);
    }

    @Override
    public boolean remove(Long violenceId) {
        return this.violenceMapper.delete(violenceId);
    }

    @Override
    public boolean modify(Violence violence) {
        return this.violenceMapper.update(violence);
    }

    @Override
    public Violence detail(Long violenceId) {
        return this.violenceMapper.selectById(violenceId);
    }

    @Override
    public Pager<Violence> list(Integer page) {
        List<Violence> datas = this.violenceMapper.selectbytime((page - 1) * Const.PAGE_SIZE, Const.PAGE_SIZE);
        int size = this.violenceMapper.size();
        return new Pager<>(datas, size, page, Const.PAGE_SIZE);
    }

    @Override
    public List<Violence> list() {
        return this.violenceMapper.selectbytime(0, 0);
    }

    @Override
    public Pager<Violence> list(Violence condition, Integer page) {
        List<Violence> datas = this.violenceMapper.selectByCondition(condition, (page - 1) * Const.PAGE_SIZE, Const.PAGE_SIZE);
        int size = this.violenceMapper.sizeByCondition(condition);
        return new Pager<>(datas, size, page, Const.PAGE_SIZE);
    }

    /**
     * 根据id查询暴行的详情
     *
     * @param violenceId
     * @return
     */

    @Override
    public Blog getViolenceDetailById(String violenceId) {
        Blog violence = (Blog) redisTemplate.opsForValue().get("violence" + violenceId);
        if (violence == null) {
            violence = violenceMapper.selectViolenceDetailById(violenceId);
            violence.setContent(textTranfromHtmlUtils.markdownToHtmlExtensions(violence.getContent()));
            redisTemplate.opsForValue().set("violence" + violenceId, violence);
        }
        return violence;
    }
}
